package com.mobile.manager.controller;

import com.mobile.manager.pojo.Apk;
import com.mobile.manager.service.IApkService;
import com.mobile.manager.utils.QRCodeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by zhangdi on 16/6/12.
 */
@Controller

@RequestMapping("/device")
public class ApkController {
    @Resource
    private IApkService apkService;
    @RequestMapping("/getApk")
    @ResponseBody
    public Apk getApk(HttpServletRequest request,Model model){
        int Id = Integer.parseInt(request.getParameter("id"));
        Apk apk = this.apkService.getApkById(Id);
        return apk;
    }
    @RequestMapping("/searchApk")
    public String searchApk(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Apk apk = new Apk();
        String search_os = "all";
        String search_version = "";
        String search_name ="";
        String search_environment = "all";
        String search_category = "all";
        if(request.getParameter("search_os")!=null){search_os=request.getParameter("search_os");}
        if(request.getParameter("search_environment")!=null){search_environment=request.getParameter("search_environment");}
        if(request.getParameter("search_name")!=null){
            search_name = java.net.URLDecoder.decode(request.getParameter("search_name"), "utf-8");
        }
        if(request.getParameter("search_version")!=null){
            search_version = java.net.URLDecoder.decode(request.getParameter("search_version"),"utf-8");

        }
        if(request.getParameter("search_category")!=null){
            search_category = java.net.URLDecoder.decode(request.getParameter("search_category"),"utf-8");

        }
        System.out.println("安装包名称是"+search_name);
        System.out.println("安装包系统是"+search_os);
        System.out.println("安装包环境是"+search_environment);
        System.out.println("安装包版本是"+search_version);

        apk.setPackage_os(search_os);
        apk.setPackage_version(search_version);
        apk.setPackage_name(search_name);
        apk.setPackage_env(search_environment);
        apk.setPackage_category(search_category);
        List<Apk> apks= this.apkService.searchApk(apk);
        model.addAttribute("packages",apks);
        return  "showAllPackage";
    }
    @RequestMapping("/editApkUI")
    public String loadAddApkUI(){
        return "editApkUI";
    }

    @RequestMapping("/insertApk")
    public String insertApk(HttpServletRequest request,Model model){
        Apk apk = new Apk();
        String QRUrl="";
        String apkName=request.getParameter("package_name");
        String apkVersion=request.getParameter("package_version");
        String apkOS=request.getParameter("OSRadios");
        String apkEnv=request.getParameter("package_env");
        String downloadUrl=request.getParameter("download_URL");
        if(apkOS.equals("Android")){ QRUrl=downloadUrl;}
        else {QRUrl=request.getParameter("QRCodeImageURL");}
        String apkCategory=request.getParameter("package_category");
        System.out.print("name="+apkName);
        System.out.print("version="+apkVersion);
        System.out.print("apkEnv="+apkEnv);

        apk.setPackage_name(apkName);
        apk.setPackage_env(apkEnv);
        apk.setPackage_version(apkVersion);
        apk.setPackage_os(apkOS);
        apk.setDownload_url(downloadUrl);
        apk.setQR_url(QRUrl);
        apk.setPackage_category(apkCategory);

        int result = this.apkService.addApk(apk);

        return  "addApkSuccess";
    }
    @RequestMapping("creatQRCodeUI")
    public String QRCodeUI(){
        return "creatQRCode";
    }
    @RequestMapping("creatQRCode")
    public String creatQRCode(HttpServletRequest request,Model model){
        QRCodeService service = new QRCodeService();
        String apkname = request.getParameter("packageName");
        String cdn = request.getParameter("cdn");
        String path = request.getContextPath();
        System.out.println("requestcontexpath==="+path);
        service.getQRCode(cdn,path,apkname);
        return "addApkSuccess";
    }
}
